<template>
  <div class="app-container">
    <div class="my-define-dialog-title">
      {{title}}
      <div class="my-define-dialog-title-btn">
        <el-button size="mini" type="primary" @click.native="submitForm">提 交</el-button>
        <el-button size="mini" @click.native="cancel(false)">取 消</el-button>
      </div>
    </div>
    <el-form ref="myForm" :model="form" :label-width="labelForm.labelWidth" :label-position="labelForm.labelPosition">
      <el-row>
        <el-col :span="24">
          <el-form-item label="密码" :prop="'LoginPwd'" :rules="{
                  required: true, message: '请输入密码', trigger: 'change'}">
            <el-input :show-password="true" type="password" v-model="form.LoginPwd"
              placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="确认密码" :prop="'LoginPwdConfirm'" :rules="{
                  required: true, message: '请再次输入密码', trigger: 'change'
                }">
            <el-input :show-password="true" type="password" v-model="form.LoginPwdConfirm"
              placeholder="请再次输入密码" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
  import {
    resetPassword
  } from '@/api/users/users'
  import {
    validatePwd
  } from '@/utils/validate'
  import defaultSettings from '@/settings'
  export default {
    name: 'user-pwd',
    props: {
      Id: {
        type: Number,
        default: 0
      },
      title: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        labelForm: defaultSettings.labelForm,
        form: {
          Id: 0,
          LoginPwd: '',
          LoginPwdConfirm: ''
        }
      }
    },
    created() {
      this.getData()
    },
    methods: {
      /** 查询列表 */
      getData() {
        this.form.Id = this.Id
      },
      submitForm() {
        var that = this;
        if (this.form.LoginPwd != this.form.LoginPwdConfirm) {
          this.$message({
            message: '两次输入密码不一致',
            type: 'warning'
          })
          return
        }
        this.$refs['myForm'].validate(valid => {
          if (valid) {
            var checkResult = validatePwd(this.form.LoginPwd)
            if (checkResult.error) {
              this.$notify.error({
                title: checkResult.error,
              });
              return;
            }
            resetPassword(this.form).then(res => {
              if (res.status) {
                this.$message({
                  message: this.form.Id > 0 ? '修改成功' : '新增成功',
                  type: 'success'
                })
                this.cancel();
              }
            })
          }
        })
      },
      cancel() {
        this.$$parent(this, "doCloseRestPwd")
      },
      handleChangeIsTenant(val) {
        if (!val) {
          this.handleChangeTenant(0)
        } else if (this.form.TenantId == 0) {
          this.form.TenantId = null;
          this.roleIdData = [];
          this.roleData = [];
        }
      },
      handleChangeTenant(val) {
        getRoleByTenant(val).then(res => {
          if (res.status) {
            this.roleData = res.outputData
            this.roleIdData = []
          }
        })
      }
    }
  }
</script>
